package com.financing.components.payment;
import java.util.Map;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.financing.components.payment.util.SecurityUtils;
import com.financing.controller.invest.BidController;
import com.financing.model.Data;
import com.financing.util.DateUtil;
import com.financing.util.Http;
import com.financing.util.Money;
import com.financing.util.XmlHelper;

public class Gateway {
	private static String URL = "https://jzh.fuiou.com";
	// private static String URL = "https://jzh-test.fuiou.com/jzh";
	private static final Logger logger = Logger.getLogger(BidController.class);
	
	public static String response(String code) {
		StringBuffer sb = new StringBuffer();
		String signature = SecurityUtils.sign(
			Data.put(
				"resp_code","0000",
				"mchnt_cd","0005840F0284514",
				"mchnt_txn_ssn",code,
				"sign","resp_code,mchnt_cd,mchnt_txn_ssn"
			)
		);
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
		.append("<ap><plain><resp_code>0000</resp_code><mchnt_cd>0005840F0284514</mchnt_cd>")
		.append("<mchnt_txn_ssn>"+code+"</mchnt_txn_ssn></plain><signature>"+signature+"</signature></ap>");
		return sb.toString(); 
	}
	
	public static Map<Object,Object> form(String url, Map<Object, Object> data) {
		data.put("url",URL+"/"+url+".action");
		data.put("ver","0.44");
		data.put("mchnt_cd","0005840F0284514");
		data.put("signature", SecurityUtils.sign(data));
		data.remove("sign");
		return data; 
	}

	public static XmlHelper pay(String from, String to, double amount, String summary) throws Exception {
		return pay(from,to,amount,summary,false,false);
	}
	
	public static XmlHelper freezePay(String from, String to, double amount, String summary, boolean isTransfer) throws Exception {
		return pay(from,to,amount,summary,isTransfer,true);
	}
	
	/**
	 * 资金划拨
	 * @param from
	 * @param to
	 * @param amount
	 * @return XmlHelper
	 * @throws Exception
	 */
	public static XmlHelper pay(String from, String to, double amount, String summary, boolean isTransfer, boolean isFreeze) throws Exception {
		String url = "/transferBu.action";
		String sign = "";
		if(isFreeze) {
			url = isTransfer ? "/transferBmuAndFreeze.action":"/transferBuAndFreeze.action";
			sign = "amt,in_cust_no,mchnt_cd,mchnt_txn_ssn,out_cust_no,rem,ver";
		}else {
			url = isTransfer ? "/transferBmu.action":"/transferBu.action";
			sign = "amt,contract_no,in_cust_no,mchnt_cd,mchnt_txn_ssn,out_cust_no,rem,ver";
		}
		
		XmlHelper res = Gateway.post(url, 
			Data.put( 
				"mchnt_txn_ssn",DateUtil.getTimeStamp(),
				"out_cust_no",from,
				"in_cust_no",to,
				"amt",(int)Money.format(amount*100),
				"contract_no","",
				"rem",summary,
				"sign",sign
			)
		);
		return res;
	}
	
	/**
	 * 划拨解冻
	 * @param from
	 * @param to
	 * @param amount
	 * @return XmlHelper
	 * @throws Exception
	 */
	public static XmlHelper unFreeze(String to, double amount, String summary) throws Exception {
		XmlHelper res = Gateway.post("/unFreeze.action", 
			Data.put( 
				"mchnt_txn_ssn",DateUtil.getTimeStamp(),
				"cust_no",to,
				"amt",(int)Money.format(amount*100),
				"rem",summary,
				"sign","amt,cust_no,mchnt_cd,mchnt_txn_ssn,rem,ver"
			)
		);
		return res;
	}
	
	/**
	 * 接口调用 
	 * @param service
	 * @param data
	 * @return
	 * @throws Exception 
	 */
	public static XmlHelper post(String service, Map<Object,Object> data) throws Exception {
		return XmlHelper.of(Gateway.contents(service, data));
	}
	
	/**
	 * 接口调用 
	 * @param service
	 * @param data
	 * @return
	 * @throws Exception 
	 */
	public static String contents(String service, Map<Object,Object> data) throws Exception {
		data.put("ver","0.44");
		data.put("mchnt_cd","0005840F0284514");
		data.put("signature", SecurityUtils.sign(data));
		data.remove("sign");
		logger.error("json==="+JSON.toJSONString(data));
		String html = Http.post(URL+service, data);
		logger.error("html==="+html);
		return html;
	}
}
